package com.jbm.jbmsupplier.activity.loadhomepage.view

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ListView
import com.handmark.pulltorefresh.library.PullToRefreshBase
import com.handmark.pulltorefresh.library.PullToRefreshListView
import com.jbm.jbmsupplier.R
import com.jbm.jbmsupplier.activity.DispushActivity
import com.jbm.jbmsupplier.activity.NewHomeActivity
import com.jbm.jbmsupplier.activity.loadhomepage.presenter.ListFrement_Prement_Con
import com.jbm.jbmsupplier.shared.SharedPreferActivity
import com.jbm.jbmsupplier.utlis.ExitActivity
import com.jbm.onehome.iface.IorderEntity
import kotlinx.android.synthetic.main.lz_frement_construction.*

/**
 * Created by 家帮忙 on 2017/8/23.
 */
class ListFrement_Construction : Fragment(), NewHomeActivity.DateTypeCon, ListFrement_View_Con {
    override fun getPulltorefre(): PullToRefreshListView {
        return pull_refresh_lists
    }

    override fun getBoolean(): Boolean {
        return isVisibleToUser1!!
    }

    override fun getCount(): Int {
        return counts!!
    }

    override fun getStatic(): String {
        return statics!!
    }
    var counts: Int? = 0
    var statics: String = "123"
    override fun setDateTypeCon(i: Int, sta: String?) {
        counts = i
//        statics = sta!!
        if (i == 0) {
            listfrementprement!!.LoadDateConstrcations(i)
        } else if (i == 1) {
            listfrementprement!!.LoadDateguidang(i)
        } else if (i == 2) {
            listfrementprement!!.LoadDateJBM(i)
        }
    }

    private var sharedPreferActivity: SharedPreferActivity? = null
    var listfrementprement: ListFrement_Prement_Con? = null
    var pull_refresh_listss: PullToRefreshListView? = null

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val view = View.inflate(activity, R.layout.lz_frement_construction, null)
        ExitActivity.getInstance().addActivity(activity)
        listfrementprement = ListFrement_Prement_Con(this, activity);
        if (counts == 0 && isVisibleToUser1 == true) {
            listfrementprement!!.LoadDateConstrcations(counts!!)
        }
        pull_refresh_listss = view.findViewById(R.id.pull_refresh_lists) as PullToRefreshListView
        sharedPreferActivity = SharedPreferActivity(activity)//保存是否更新单子
        sharedPreferActivity?.setBoolearorder(false)
        pullUpRefresh()
        return view
    }
    private fun pullUpRefresh() {
        pull_refresh_listss?.setEmptyView(lv_defaultorder)
        pull_refresh_listss?.setOnLastItemVisibleListener(PullToRefreshBase.OnLastItemVisibleListener {
            //预加载
            if (counts == 0) {
                listfrementprement!!.LoadDateConstrcations(5)
            } else if (counts == 1) {
                listfrementprement!!.LoadDateguidang(5)
            } else if (counts == 2) {
                listfrementprement!!.LoadDateJBM(5)
            }

        })
        pull_refresh_listss?.setOnRefreshListener(object : PullToRefreshBase.OnRefreshListener2<ListView> {
            override fun onPullDownToRefresh(refreshView: PullToRefreshBase<ListView>) {//下拉
                if (counts == 0) {
                    listfrementprement!!.LoadDateConstrcations(3)
                } else if (counts == 1) {
                    listfrementprement!!.LoadDateguidang(3)
                } else if (counts == 2) {
                    listfrementprement!!.LoadDateJBM(3)
                }


            }

            override fun onPullUpToRefresh(refreshView: PullToRefreshBase<ListView>) {//上拉
                if (counts == 0) {
                    listfrementprement!!.LoadDateConstrcations(4)
                } else if (counts == 1) {
                    listfrementprement!!.LoadDateguidang(4)
                } else if (counts == 2) {
                    listfrementprement!!.LoadDateJBM(4)
                }
            }

        })
        pull_refresh_listss?.setOnItemClickListener(AdapterView.OnItemClickListener { parent, view, position, id ->
            val or = parent.adapter.getItem(position) as IorderEntity
            val intent = Intent(activity, DispushActivity::class.java)
            val bundle = Bundle()
            bundle.putSerializable("order", or)
            if (counts == 1 || counts == 2) {

                bundle.putString("vis", "1")//标识 归档 和 家帮忙 单子 如果要是temp类型就隐藏  派单和分配家帮忙
            } else if (counts == 0) {
                bundle.putString("vis", "2")//标识归档 和 家帮忙 如果要是temp类型就隐藏  派单和分配家帮忙
            }
            bundle.putString("tag", "1")//标识是施工单子的详情 还是  管理端
            sharedPreferActivity?.setBoolearorder(false)
            intent.putExtras(bundle)
            //标志当前是哪个页面
            startActivityForResult(intent, 100)
        })


    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            100 -> {
                if (resultCode != Activity.RESULT_OK) {
                    return
                }

                listfrementprement!!.LoadDateConstrcations(counts!!)
            }
        }
    }


    private var isVisibleToUser1: Boolean? = false

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        isVisibleToUser1 = isVisibleToUser

    }

    override fun onResume() {
        super.onResume()
        if (sharedPreferActivity?.boolearorder == true) {
            listfrementprement!!.LoadDateConstrcations(counts!!)

        }

    }

}